package com.shou.controller.admin;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shou.BusinessException;
import com.shou.CommonVar;
import com.shou.entity.Administrator;
import com.shou.service.impl.AdministratorService;
import com.shou.util.CipherUtil;

@Controller("adminAdministratorController")
@RequestMapping(value = "/admin/administrator")
public class AdministratorController extends com.shou.controller.MyController {
	@Autowired
	AdministratorService administratorService;
	
	/**
	 * 后台首页
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/index")
	public String index(Model model,HttpServletRequest request) throws Exception {
		return "/admin/index";
	}
	
	/**
	 * 管理员登录
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/login")
	public ResponseEntity<String> login(Model model, HttpServletRequest request, String username, String password, String validate) throws Exception {
		Boolean success = true;
		String msg = "";
		
		if(username==null || username.isEmpty()) {
			msg = "请输入用户名";
			success = false;
		}
		else if(password==null || password.isEmpty()) {
			msg = "请输入密码";
			success = false;
		}
		else if(validate == null || validate.isEmpty()){
			msg = "请输入验证码";
			success = false;
		}
		else {
			try{				
				administratorService.login(request, username, password, validate);
			}
			catch(BusinessException be){
				msg = be.getErrorMessage();
				success =false;
			}
			catch(Exception e) {
				msg = e.getMessage();
				success = false;
				e.printStackTrace();
			}			
		}
		return renderMsg(success, msg);
	}
	
	/**
	 * 管理员用户登出
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout")
	public String logout(Model model, HttpServletRequest request)throws Exception {
		request.getSession().removeAttribute(CommonVar.SESSION_ADMIN);
		request.getSession().invalidate();
		return "redirect:/public/login.htm";
	}
	
	/**
	 * 修改密码
	 * @param administrator
	 * @param newPwd
	 * @param newPwd2
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/changePwd")
	public ResponseEntity<String> changePwd(Model model, HttpServletRequest request, String opassword, String password1, String password2){
		Boolean success = true;
		String msg = "修改密码成功";
		
		if(opassword==null || opassword.isEmpty()) {
			msg = "请输入登录密码";
			success = false;
		}
		else if(password1 ==null || password1.isEmpty()) {
			msg = "请输入新密码";
			success = false;
		}		
		else if(password1.length() < 4 || password1.length() > 16){
			msg = "密码长度应该大于4位小于16位";
			success = false;
		}
		else if(!password1.equalsIgnoreCase(password2)) {
			msg = "两次输入的密码不一致";
			success = false;
		}
		else {
			try{
				administratorService.modifyPwd(request, opassword,password1);
			}
			catch(BusinessException be){
				msg = be.getErrorMessage();
				success = false;
			}
			catch(Exception e){
				msg = e.getMessage();
				success = false;
			}
		}
		return renderMsg(success, msg);
	}
	
}
